#include "fensaisieprof.h"
#include "ui_fensaisieprof.h"

FenSaisieProf::FenSaisieProf(QWidget *parent) :
    QDialog(parent)

{
    setupUi(this);

    QObject::connect(buttonBox,SIGNAL(accepted()),this,SLOT(BtnOK()));
    QObject::connect(buttonBox,SIGNAL(rejected()),this,SLOT(BtnCancel()));
    QObject::connect(ButtonAjouter,SIGNAL(clicked()),this,SLOT(AjouterInstru()));
    QObject::connect(ButtonSupprimer,SIGNAL(clicked()),this,SLOT(SupprimerInstru()));   
}
FenSaisieProf::~FenSaisieProf()
{
    delete this;
}
void FenSaisieProf::SetData(QStringList listeInstru, ListeProf &listeProf, bool modifierProf, int indexProf)
{
    m_listeInstru = listeInstru;
    m_listeProf = &listeProf;

    ModeSaisie = modifierProf;

    if (ModeSaisie)
        ModifierProfIsON(indexProf);

    listeInstruComboBox->addItems(m_listeInstru);
}

void FenSaisieProf::BtnOK()
{
    if (!nomLineEdit->text().isEmpty() &  !prNomLineEdit->text().isEmpty() && !(listeInstruListWidget->count() < 1))
    {

        Info i1(nomLineEdit->text(),prNomLineEdit->text(),adresseLineEdit->text(),codePostalLineEdit->text(),villeLineEdit->text(),tLPhoneFixeLineEdit->text(),tLPhonePortableLineEdit->text(),eMailLineEdit->text());
        QStringList sl1;
        for (int i = 0; i < listeInstruListWidget->count();i++)
        {
            sl1.append(listeInstruListWidget->item(i)->text());
        }

        Prof pf1(i1,sl1);

        if (ModeSaisie)
        {
            if (m_listeProf->count() >= LIndex)
            {
                m_listeProf->replace(LIndex,pf1);
            }
        }
        else
        {
            if (!m_listeProf->isEmpty())
            {
                if (m_listeProf->Existe(pf1.getInfo().NomEtPrenom()))
                {
                    QMessageBox::information(NULL,ERREUR, CEPROFEXISTEDEJA);
                }
                else
                {
                    m_listeProf->append(pf1);
                }
            }
            else
            {
               m_listeProf->append(pf1);
            }
        }
    }
    else
    {
        QMessageBox::information(NULL, ERREUR, VOUSDEVEZOBLIGATOIREMENTSAISIRUNNOMUNPRENOMETUNINSTRUMENT);
        return;
    }
    this->close();
}
void FenSaisieProf::BtnCancel()
{
    this->close();


}
void FenSaisieProf::ModifierProfIsON(int monIndex)
{
    nomLineEdit->setText(m_listeProf->at(monIndex).getInfo().getNom());
    prNomLineEdit->setText(m_listeProf->at(monIndex).getInfo().getPrenom());
    adresseLineEdit->setText(m_listeProf->at(monIndex).getInfo().getAdresse());
    codePostalLineEdit->setText(m_listeProf->at(monIndex).getInfo().getCodePostal());
    villeLineEdit->setText(m_listeProf->at(monIndex).getInfo().getVille());
    tLPhoneFixeLineEdit->setText(m_listeProf->at(monIndex).getInfo().getTelFixe());
    tLPhonePortableLineEdit->setText(m_listeProf->at(monIndex).getInfo().getTelPortable());
    eMailLineEdit->setText(m_listeProf->at(monIndex).getInfo().getEmail());

    listeInstruListWidget->addItems(m_listeProf->at(monIndex).getListeInstrus());

    ModeSaisie = true;
    LIndex = monIndex;


}
void FenSaisieProf::AjouterInstru()
{
    if (listeInstruComboBox->currentIndex() >= 0)
    {
        listeInstruListWidget->addItem(listeInstruComboBox->currentText());

    }
}
void FenSaisieProf::SupprimerInstru()
{

    if (listeInstruListWidget->currentItem() != NULL)
    {
        listeInstruListWidget->takeItem(listeInstruListWidget->currentRow());
    }
}
